
/**
 * 接口日志中间件
 * @param {string} action 行为
 */
const ApiLoggerMiddleware = (action) => {
    return async function (ctx, next) {
        console.log("当前操作行为：", action);
        await next();
        console.log(ctx);
    }
}

/**
 * 接口描述
 * @param {*} description 
 * @returns 
 */
const ApiDescribe = (description) => {
    return async (ctx, next) => {
        // 将描述存入ctx供后续使用
        ctx.apiDescription = description || 'Unknown';
        await next();
        const response = ctx.response;
        let stateText = '成功'
        if (response.status !== 200) {
            stateText = '失败'
        }
        if (response.body && response.body.code !== 1000) {
            stateText = '失败'
        }
        ctx.apiDescription = description + stateText;
    }
}


module.exports = { ApiDescribe, ApiLoggerMiddleware };